package com.longrise.android.byjk.utils;

import android.text.TextUtils;

import com.longrise.android.byjk.model.CourseBean;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by sujizhong on 16/5/26.
 * <p/>
 * mList中用于存放某个课程未学习完成的课件队列
 */
public class CourseUtil {

    private static CourseUtil mCourseUtil = null;

    private static final ArrayList<CourseBean> mList = new ArrayList<>();

    private static CourseUtil getInstance() {
        if (mCourseUtil == null) {
            synchronized (CourseUtil.class) {
                if (mCourseUtil == null) {
                    mCourseUtil = new CourseUtil();
                }
            }
        }
        return mCourseUtil;
    }

    /**
     * @param haspass
     * @param papass  课件习题是否通过
     */
    public void add(String cwid, String groupid, String pathNo, boolean haspass, boolean papass, String name) {
        CourseBean bean = new CourseBean();
        bean.mCwid = cwid;
        bean.mGroupId = groupid;
        bean.mPathNo = pathNo;
        bean.mHasqa = haspass;
        bean.mVideoPass = papass;
        bean.mName = name;
        mList.add(bean);
    }

    /**
     * 始终获取第一条
     */
    public CourseBean get() {
        if (mList != null && mList.size() > 0) {
            CourseBean bean = mList.get(0);
            return bean;
        }
        return null;
    }

    /**
     * 根据课件id移除
     */
    public void remove(String cwid) {
        if (mList == null || TextUtils.isEmpty(cwid)) return;
        int size = mList.size();
        if (size > 0) {
            Iterator<CourseBean> iterator = mList.iterator();
            while (iterator.hasNext()) {
                CourseBean bean = iterator.next();
                if (bean.mCwid.equals(cwid)) {
                    mList.remove(bean);
                    break;
                }
            }
//            for (CourseBean bean : mList) {
//                if (bean.mCwid.equals(cwid)) {
//                    mList.remove(bean);
//                    break;
//                }
//            }
        }
    }

    public void clearCourse() {
        if (mList != null && mList.size() > 0) {
            mList.clear();
        }
    }

    public void release() {
        mCourseUtil = null;
        if (mList != null) {
            mList.clear();
        }
    }
}
